/**
 * Created by XianTao on 2015/3/12
 *
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package com.taotao.game;

import com.taotao.rop.util.DateTimeUtil;
import com.taotao.wechat.game.SlotGame;
import com.taotao.wechat.util.ValidateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Date;

@Controller
@RequestMapping(value = "/slot")
public class SlotController extends ChanceGameController {

    @Override
    protected Object[][] getPrizeArray() {
        return new Object[][]{
                //id, rate, prize, prizeInfo
                {1, 2, "3,3,3", "200元电子券"},
                {2, 4, "4,4,4", "100元电子券"},
                {3, 8, "5,5,5", "50元电子券"},
                {4, 40, "6,6,6", "20元电子券"},
                {5, 80, "7,7,7", "5元话费"},
                {6, 200, "1,1,1", "3元话费"},
                {7, 1000, "2,2,2", "1元话费"},
                {0, 8666, "", "谢谢参与"}
        };
    }

    @Override
    protected String msgWrongParameter() {
        return "请先关注【内蒙古移动E生活】，公众微信号再参加活动~";
    }

    @Override
    protected int maxWechatCount() {
        return 1;
    }

    @Override
    protected int maxWechatMemberCount() {
        return 1;
    }

    @Override
    protected String msgAlreadyMemberJoin() {
        return new SlotGame().msgAlreadyJoin();
    }

    @Override
    protected String msgAlreadyJoin() {
        return new SlotGame().msgAlreadyJoin();
    }

    @Override
    protected int maxTelCount() {
        return 1;
    }

    @Override
    protected int maxTelMemberCount() {
        return 1;
    }

    @Override
    protected String msgOverMaxTelCount() {
        return new SlotGame().msgTelAlreadyJoin();
    }

    @RequestMapping(value = "/end", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    @Override
    public Object gameEnd(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        SlotGame slotGame = new SlotGame();

        String appId = request.getParameter("appId");
        String openId = request.getParameter("openId");
        String tel = request.getParameter("tel");

        //判断游戏是否在活动期，判断参数是否正确
        Date now = new Date();
        String msg;     //网页提示信息
        String title;   //网页提示信息的标题
        String prizeId = ""; //中奖项id
        String prize = ""; //中奖项
        String prizeInfo = ""; //中奖项的info提示信息

        //判断是否在活动的日子
        Date nowStart = new Date(DateTimeUtil.getDateStartMillis(new Date()));
        String activeDay = slotGame.getActiveDay();
        if (DateTimeUtil.compareDate(slotGame.getStartDate(), now) == 1) {
            msg = slotGame.getMsgGameNotStart();
            title = "信息提示";
        } else if (DateTimeUtil.compareDate(slotGame.getEndDate(), now) == -1) {
            msg = slotGame.getMsgGameIsEnd();
            title = "信息提示";
        } else if (activeDay == null || DateTimeUtil.compareDate(activeDay, nowStart) != 0){
            msg = slotGame.msgNotInActiveDay();
            title = "信息提示";
        } else if (StringUtils.isBlank(appId) || StringUtils.isBlank(openId) || StringUtils.isBlank(tel)) {
            msg = msgWrongParameter();
            title = "错误提示";
        } else {
            int telType = ValidateUtil.isYiDong(tel);
            if (telType != 0) {
                if (telType == -2) {
                    msg = msgWrongParameter();
                    title = "错误提示";
                } else if (telType == -1) {
                    msg = slotGame.getMsgTelNotKnow();
                    title = "信息提示";
                }  else if (telType == -3) {
                    msg = slotGame.getMsgTelException();
                    title = "信息提示";
                } else {
                    msg = slotGame.getMsgTelNotNeiMengCMCC();
                    title = "信息提示";
                }
            } else {
                //正确的内蒙移动手机号

                //判断微信号是否已经参加过
                int joinToday = ValidateUtil.hasJoinToday(appId, openId, slotGame.getNickName(), maxWechatCount(), maxWechatMemberCount());
                if (joinToday == ValidateUtil.NO_MEMBER_COUNT) {
                    msg = msgAlreadyMemberJoin();
                    title = "谢谢参与";
                } else if (joinToday == ValidateUtil.NO_COUNT) {
                    msg = msgAlreadyJoin();
                    title = "谢谢参与";
                } else {
                    //判断手机号是否已经超过抽奖次数
                    if (ValidateUtil.hasTelJoinCountToday(tel, appId, openId, slotGame.getNickName(), maxTelCount(), maxTelMemberCount()) == ValidateUtil.NO_COUNT) {
                        msg = msgOverMaxTelCount();
                        title = "谢谢参与";
                    } else {
                        //正确参加游戏
                        Integer awardIndex = getAwardIndex(getPrizeArray(), 1);
                        String result = (String) getPrizeArray()[awardIndex][3];
                        String grade = "" + getPrizeArray()[awardIndex][0];
                        //存入数据库
                        if (endGame(appId, openId, slotGame.getNickName(), tel, result, grade)) {
                            title = result;
                            if ("0".equalsIgnoreCase(grade)) {
                                msg = "据说穿红内裤，可以提升中奖概率哦";
                            } else {
                                msg = "好运滚滚来～";
                            }
                            prizeId = "" + getPrizeArray()[awardIndex][0];
                            prize = (String) getPrizeArray()[awardIndex][2];
                            prizeInfo = (String) getPrizeArray()[awardIndex][3];
                        } else {
                            msg = msgWrongParameter();
                            title = "错误提示";
                        }
                    }
                }
            }
        }

        return "{\"id\":\"" + prizeId + "\", \"details\":\"" + msg + "\", \"title\":\"" + title
                + "\", \"prizeInfo\":\"" + prizeInfo
                + "\", \"prize\":\"" + prize + "\"}";
    }

    @RequestMapping(value = "/prize", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    @Override
    public Object getMyPrizes(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {
        String appId = request.getParameter("appId");
        String openId = request.getParameter("openId");
        return getPrizes(appId, openId, new SlotGame().getNickName());
    }
}
